package com.three.lab;

import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.content.res.Configuration;
import android.util.Log;

import androidx.multidex.MultiDex;

import com.three.lab.util.LanguageUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * appliation
 * created by lqd on 2024-06-05.
 */
public class MyApplication extends Application {

    private static String TAG = MyApplication.class.getSimpleName();
    private static int popupMaxHeight = 500;
    private static Context context;
    public static MyApplication instances = null;
    public static List<Activity> activityList = new ArrayList<>();
    public static MyApplication getInstances(){
        if (instances == null) {
            Log.w(TAG,"[MyApplication] instance is null.");
        }
        return instances;
    }

    public static long getTimesTamp(){
        return System.currentTimeMillis();
    }

    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        try {
            MultiDex.install(this);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        LanguageUtils.setLocal(context);
    }

    @Override
    public void onCreate() {
        super.onCreate();
        instances = this;
        context = getApplicationContext();
        activityList = new ArrayList<>();
        //获取屏幕高度
        int height = context.getResources().getDisplayMetrics().heightPixels;
        //设置弹出框最高高度
        popupMaxHeight = height * 4 / 5;
        //Java
        //MapsInitializer.updatePrivacyShow(context,true,true);
        //MapsInitializer.updatePrivacyAgree(context,true);
    }

    public static int getPopupHeight(){
        return popupMaxHeight;
    }

    public static Context getContext(){
        return context;
    }

    @Override
    public void onTerminate() {
        super.onTerminate();
        if (activityList != null) {
            activityList.clear();
        }
    }

    public  void pushTask(Activity activity) {
        activityList.add(activity);
    }

    public  void removeTask(Activity activity) {
        if (activityList.contains(activity)) {
            activityList.remove(activity);
        }
    }

    public  void removeTask(int taskIndex) {
        if (activityList.size() > taskIndex)
            activityList.remove(taskIndex);
    }

    public  void removeAll() {
        try {
            for (Activity activity : activityList) {
                if (!activity.isFinishing()) {
                    activity.finish();
                }
            }
            activityList.clear();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
